package com.bawei.module_car_space


import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.lifecycleScope

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.module_car_space.adapter.SpaceAdapter
import com.bawei.module_car_space.databinding.ActivityCarSpaceBinding
import com.bawei.module_car_space.model.SpaceModel
import com.bawei.module_car_space.model.SpaceModelItem
import com.bawei.module_car_space.ui.SpaceViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch


@AndroidEntryPoint
@Route(path = Router.Ui.CarSpaceActivity)
class CarSpaceActivity : BaseActivity<SpaceViewModel,ActivityCarSpaceBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_car_space
    override val mViewModel: SpaceViewModel by viewModels()
    private val mSpaceAdapter by lazy { SpaceAdapter() }
    // 1. 声明本地数据源（模拟从ViewModel获取的原始数据）
    private val localSpaceList = mutableListOf<SpaceModelItem>()
    override fun initView() {
        mBinding.rvSpace.apply {
            adapter = mSpaceAdapter
            layoutManager = LinearLayoutManager(this@CarSpaceActivity)
        }
        mBinding.carSpaceToolbar.spaceAdd.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.EditActivity).navigation()
        }
    }

    override fun initListener() {
        mViewModel.getSpaceData("标准车位")
    }

    override fun initData() {
        lifecycleScope.launch {
            mViewModel.mSpaceState.collect { newData ->
                Log.d("CarSpaceActivity", "收到数据: $newData") // 打印数据
                mSpaceAdapter.submitList(mViewModel.mSpaceListData)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getSpaceData("标准车位")
    }

}